package easybooking.db;

import easybooking.data.Airline;
import easybooking.data.AirlineImpl;
import java.rmi.RemoteException;
import java.util.Vector;
import java.sql.*;

/**
 * Mapper-Class, which displays <code>Airline</code> objects in a relational
 * database. Therefore a collection of methods to search, create and modify
 * will be provided.
 * The mapping is bidirectional, which means, that objects could be transformed
 * into database-structures and database-structures into objects.
 *
 * @author Mark Roman Stehle
 */
// <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
// #[regen=yes,id=DCE.09DFE639-C9BF-3647-E4DA-E652E9254625]
// </editor-fold>
public class AirlineMapper extends Mapper {

    /**
     * The class <code>AirlineMapper</code> will be instantiated only once.
     * This is called <b>Singleton</b>, because of the <code>static</code>-tag,
     * the variable exists only once for all instances of this class. The
     * variable saves the only instance of this class.
     *
     * @see airlineMapper()
     */
    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.52C8E148-8AFB-BC5B-7893-D4CC737EFC41]
    // </editor-fold> 
    private static AirlineMapper airlineMapper = null;

    /**
     * The protected constructor prevents the possibility to generate a new
     * instance of this class.
     */
    protected AirlineMapper() {
    }

    /**
     * The static method can be called with
     * <code>AirlineMapper.airlineMapper()</code>.
     * This guarantees the singleton property by checking so that only one
     * instance of the <code>AirlineMapper</code> exists.

     * <b>Conclusion:</b> The <code>AirlineMapper</code> should not be
     * instantiated with <code>new</code>, but rather by calling the static
     * method.

     * @return The <code>AirlineMapper</code> object.
     * @see airlineMapper
     */
    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.D144130D-F13B-5575-F84A-E9D044D7D953]
    // </editor-fold> 
    public static AirlineMapper airlineMapper() {
        if (airlineMapper == null) {
            airlineMapper = new AirlineMapper();
        }
        return airlineMapper;
    }

    /**
     * Searching an airline with given key, since the key is distinct only one
     * object will be returned.
     *
     * @param id primary key (->DB)
     * @return Airline object, which corresponds the given key,
     * <code>null</code> for a non existing database tupel.
     */
    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.EA5CFDD2-DAB9-AA67-93AE-E3995D02D678]
    // </editor-fold> 
    public Airline findByKey(int id) {
        Connection con = DBConnection.connection();
        try {
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(
                    "SELECT airlineId, name, initials, isActive FROM Airline " +
                    "WHERE airlineId=" + id);
            if (rs.next()) {
                try {
                    Airline a = new AirlineImpl();
                    a.setAirlineId(rs.getInt("airlineId"));
                    a.setName(rs.getString("name"));
                    a.setInitials(rs.getString("initials"));
                    a.setIsActive(rs.getBoolean("isActive"));
                    return a;
                } catch (Exception ex) {
                    ex.printStackTrace();
                    return null;
                }
            }
        } catch (SQLException e2) {
            e2.printStackTrace();
            return null;
        }
        return null;
    }

    /**
     * Selecting all active airlines from the database.
     *
     * @return A vector of airline objects, which includes all active airlines.
     * In case of an exception, an empty vector will be returned.
     */
    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.E8C03A59-76FA-3517-2619-5B498BDD45F9]
    // </editor-fold> 
    public Vector<Airline> findAll() {
        Connection con = DBConnection.connection();
        Vector<Airline> result = new Vector<Airline>();
        try {
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(
                    "SELECT airlineId, name, initials, isActive FROM Airline " +
                    "WHERE isActive = true " +
                    "ORDER BY airlineId ASC");
            while (rs.next()) {
                try {
                    Airline a = new AirlineImpl();
                    a.setAirlineId(rs.getInt("airlineId"));
                    a.setName(rs.getString("name"));
                    a.setInitials(rs.getString("initials"));
                    a.setIsActive(rs.getBoolean("isActive"));
                    result.addElement(a);
                } catch (Exception ex) {
                    ex.printStackTrace();
                    break;
                }
            }
        } catch (SQLException e2) {
            e2.printStackTrace();
        }
        return result;
    }

    /**
     * Inserting an <code>Airline</code> object into the database. Before that,
     * the primary key of the given object will be verified und corrected.
     *
     * @param a the object to be saved
     * @return The given object, with corrected primary key.
     */
    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.2535AF48-8A30-8194-AD04-8850C8C0D5AC]
    // </editor-fold> 
    public Airline insert(Airline a) {
        Connection con = DBConnection.connection();
        try {
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(
                    "SELECT MAX(airlineId) AS maxAirlineId " +
                    "FROM Airline");
            if (rs.next()) {
                try {
                    a.setAirlineId(rs.getInt("maxAirlineId") + 1);
                    stmt = con.createStatement();
                    stmt.executeUpdate("INSERT INTO Airline (airlineId, name, initials, isActive) " +
                            "VALUES (" +
                            a.getAirlineId() + ",'" +
                            a.getName() + "','" +
                            a.getInitials() + "','" +
                            booleanToInt(a.getIsActive()) + "')");
                } catch (RemoteException ex) {
                    ex.printStackTrace();
                }
            }
        } catch (SQLException e2) {
            e2.printStackTrace();
        }
        return a;
    }

    /**
     * Rewriting an object into the database.
     *
     * @param a the object which sould be written into the database
     * @return The given object.
     */
    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.AE09B511-ABA6-2345-D842-A1A18F5FE354]
    // </editor-fold> 
    public Airline update(Airline a) {
        Connection con = DBConnection.connection();
        try {
            Statement stmt = con.createStatement();
            stmt.executeUpdate("UPDATE Airline " +
                    "SET name=\"" + a.getName() + "\", " +
                    "initials=\"" + a.getInitials() + "\", " +
                    "isActive=\"" + booleanToInt(a.getIsActive()) + "\" " +
                    "WHERE airlineId=" + a.getAirlineId());
        } catch (SQLException e2) {
            e2.printStackTrace();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        return a;
    }

    /**
     * Selecting all active airlines from the database with a certain name,
     * which is given through a parameter.
     *
     * @param name the name or part of a name which should be searched for
     * @return A vector of airline objects, which includes all active airlines
     * with a certain name.
     * In case of an exception, an empty vector will be returned.
     */
    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.B4EC0E29-6733-A115-523E-8FA68FBF3D16]
    // </editor-fold> 
    public Vector<Airline> findByName(String name) {
        Connection con = DBConnection.connection();
        Vector<Airline> result = new Vector<Airline>();
        try {
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(
                    "SELECT airlineId, name, initials, isActive FROM Airline " +
                    "WHERE name LIKE '%" + name + "%' " +
                    "AND isActive = true " +
                    "ORDER BY airlineId ASC");
            while (rs.next()) {
                try {
                    Airline a = new AirlineImpl();
                    a.setAirlineId(rs.getInt("airlineId"));
                    a.setName(rs.getString("name"));
                    a.setInitials(rs.getString("initials"));
                    a.setIsActive(rs.getBoolean("isActive"));
                    result.addElement(a);
                } catch (Exception ex) {
                    ex.printStackTrace();
                    break;
                }
            }
        } catch (SQLException e2) {
            e2.printStackTrace();
        }
        return result;
    }

    /**
     * Selecting all active airlines from the database with certain initials,
     * which are given through a parameter.
     *
     * @param initials the initials or part of the initials which should be
     * searched for
     * @return A vector of airline objects, which includes all active airlines
     * with certain initials.
     * In case of an exception, an empty vector will be returned.
     */
    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.BE627711-B62F-D2EE-CCE7-B2905B94FCE5]
    // </editor-fold> 
    public Vector<Airline> findByInitials(String initials) {
        Connection con = DBConnection.connection();
        Vector<Airline> result = new Vector<Airline>();
        try {
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(
                    "SELECT airlineId, name, initials, isActive FROM Airline " +
                    "WHERE initials LIKE '%" + initials + "%' " +
                    "AND isActive = true " +
                    "ORDER BY airlineId ASC");
            while (rs.next()) {
                try {
                    Airline a = new AirlineImpl();
                    a.setAirlineId(rs.getInt("airlineId"));
                    a.setName(rs.getString("name"));
                    a.setInitials(rs.getString("initials"));
                    a.setIsActive(rs.getBoolean("isActive"));
                    result.addElement(a);
                } catch (Exception ex) {
                    ex.printStackTrace();
                    break;
                }
            }
        } catch (SQLException e2) {
            e2.printStackTrace();
        }
        return result;
    }
}

